package com.example.custom_view.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author yaotianxue
 * @description
 * @date 2024/9/28
 */
public class MyViewGroup extends ViewGroup {
    public static final String TAG = "MyViewGroup";
    public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyViewGroup(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        for (int i = 0 ;i< getChildCount();i++){
            View child = getChildAt(i);
            //测量子控件的大小
            LayoutParams layoutParams = child.getLayoutParams();
            int mode = 0;
            int size = 0;
            if(layoutParams.width == LayoutParams.WRAP_CONTENT){
                mode = MeasureSpec.AT_MOST;
                size = layoutParams.width;
            }else if(layoutParams.width == LayoutParams.MATCH_PARENT){
                mode = MeasureSpec.EXACTLY;
                size = layoutParams.width;
            }else{
                mode = MeasureSpec.EXACTLY;
                size = layoutParams.width;
            }

            int mode_ = 0;
            int size_ = 0;
            if(layoutParams.height == LayoutParams.WRAP_CONTENT){
                mode_ = MeasureSpec.AT_MOST;
                size_ = layoutParams.height;
            }else if(layoutParams.width == LayoutParams.MATCH_PARENT){
                mode_ = MeasureSpec.EXACTLY;
                size_ = layoutParams.height;
            }else{
                mode_ = MeasureSpec.EXACTLY;
                size_ = layoutParams.height;
            }
            child.measure(MeasureSpec.makeMeasureSpec(mode, size), MeasureSpec.makeMeasureSpec(mode_, size_));
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //垂直摆放
//        int top = 0;//累计高
//        for (int i = 0 ;i< getChildCount();i++){
//            View child = getChildAt(i);
//            //摆放子控件的位置
//            Log.d(TAG, "onLayout: l:"+0+" t:"+top+" r:"+child.getMeasuredWidth()+" b:"+top+child.getMeasuredHeight());
//            child.layout(0,top,child.getMeasuredWidth(),top+child.getMeasuredHeight());
//            top += child.getMeasuredHeight();
//        }
        //水平摆放
//        int left = 0;//累计宽
//        for (int i = 0 ;i< getChildCount();i++){
//            View child = getChildAt(i);
//            //摆放子控件的位置
//            child.layout(left,0,left+child.getMeasuredWidth(),child.getMeasuredHeight());
//            left += child.getMeasuredWidth();
//        }
        //流式布局：自动换行
        int left = 0;//累计宽
        int top = 0;
        for (int i = 0 ;i< getChildCount();i++){
            View child = getChildAt(i);
            //摆放子控件的位置：判断剩余空间是否足够
            if(left + child.getMeasuredWidth() > getMeasuredWidth()){//换行
                left = 0;
                top += child.getMeasuredHeight();
                Log.d(TAG, i+"onLayout: 换行"+" l:"+left + " top:"+top);
            }
            child.layout(left,top,left+child.getMeasuredWidth(),top+child.getMeasuredHeight());
            left += child.getMeasuredWidth();
        }
    }
}
